<?php
include_once("../includes/common.php");
include_once("../class/User.class.php");
include_once("../class/Action.class.php");
include_once("../class/Explore.class.php");
require_once("../islogin.php");

if ($isadmin!=1){
    header('Location: /');
    exit;
}

$menuavatar = !$user_info["message"]["avatar"] ? "/data/avatar/default.jpg" : $user_info["message"]["avatar"];
$pageName = "笔记数据操控";

$action = new Action();
$explore = new Explore();

if($_GET['nid']&&!isUid($_GET['nid'])){
    exit('error nid');
}
if($_GET['nid']){
    $res = json_decode($explore->getIdNotes($_GET['nid']),true);
    $statist = json_decode($explore->getNoteStatistics($_GET['nid']),true);
    if(!$res){
        header('Location: note_control.php');
        exit('不存在的nid');
    }

    $bot_num = json_decode($action->getBotActions($_GET['nid']),true);
    //var_dump($bot_num);
}



if (isset($_GET['del'])) {
    // 在这里执行删除操作
    $explore->hideNote($_GET['uid'], $_GET['del']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER']; // 获取来路页面的 URL
    header("Location: $referrer"); // 重定向到来路页面
    exit();
}
if (isset($_GET['recover'])) {
    // 在这里执行删除操作
    $explore->recoverNote($_GET['recover']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER']; // 获取来路页面的 URL
    header("Location: $referrer"); // 重定向到来路页面
    exit();
}

if (isset($_GET['realdel'])) {
    // 在这里执行删除操作
    $explore->deleteNote($_GET['realdel']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER'];
    header("Location: $referrer");
    exit();
}
if (isset($_GET['status'])) {
    // 在这里执行删除操作
    $explore->upNoteCheckResult($_GET['nid'],$_GET['status']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER'];
    header("Location: $referrer");
    exit();
}
if (isset($_GET['admin_edit'])) {
    $Temp_u_token = json_decode($user->TempUidlogin($_GET['uid']),true)['message'];

    setcookie("save_me_token", $_COOKIE['admin_token'], time() + (86400 * COOKIE_OVERDUE_DAY), "/");
    setcookie("u_token", $Temp_u_token, time() + (86400 * COOKIE_OVERDUE_DAY), "/");

    header("Location: /control/edit.php?nid=".$_GET['admin_edit']);
    exit();
}


//操纵数据
// 解析参数
$addor = isset($_GET['addor']) ? $_GET['addor'] : null;
$type = isset($_GET['type']) ? $_GET['type'] : null;
$num = isset($_GET['num']) ? $_GET['num'] : null;
$nid = isset($_GET['nid']) ? $_GET['nid'] : null;

// 检查所有参数是否不为空
if (isset($addor, $type, $num, $nid)) {

    $userData = $bot_num['user_data'];

    // 检查 num 是否超过可用的 bot 数量
    if ($addor == 0) { // 增加操作
        if ($type == 0) { // like
            $availableBots = count($userData["no_like_botid"]);
            $num = min($num, $availableBots); // 确保 num 不超过可用的 no_like_botid 数量

            // 从 no_like_botid 中随机抽取 num 个
            $selectedBots = array_rand($userData["no_like_botid"], $num);
            $selectedBots = is_array($selectedBots) ? $selectedBots : [$selectedBots]; // 确保是数组
            //var_dump($selectedBots);exit;
            foreach ($selectedBots as $botIndex) {
                $uid = $userData["no_like_botid"][$botIndex]; // $uid 是随机抽取的 botid
                $action->userLikeAction($uid, $nid, null, 0); // $nid 是 get 请求传入的值
            }
        } elseif ($type == 1) { // star
            $availableBots = count($userData["no_star_botid"]);
            $num = min($num, $availableBots); // 确保 num 不超过可用的 no_star_botid 数量

            // 从 no_star_botid 中随机抽取 num 个
            $selectedBots = array_rand($userData["no_star_botid"], $num);
            $selectedBots = is_array($selectedBots) ? $selectedBots : [$selectedBots]; // 确保是数组

            foreach ($selectedBots as $botIndex) {
                $uid = $userData["no_star_botid"][$botIndex]; // $uid 是随机抽取的 botid
                $action->userLikeAction($uid, $nid, null, 1); // $nid 是 get 请求传入的值
            }
        }
    } elseif ($addor == 1) { // 减少操作
        if ($type == 0) { // like
            $availableBots = count($userData["like_botid"]);
            $num = min($num, $availableBots); // 确保 num 不超过可用的 like_botid 数量

            // 从 like_botid 中随机抽取 num 个
            $selectedBots = array_rand($userData["like_botid"], $num);
            $selectedBots = is_array($selectedBots) ? $selectedBots : [$selectedBots]; // 确保是数组

            foreach ($selectedBots as $botIndex) {
                $uid = $userData["like_botid"][$botIndex]; // $uid 是随机抽取的 botid
                $action->userLikeAction($uid, $nid, null, 0); // $nid 是 get 请求传入的值
            }
        } elseif ($type == 1) { // star
            $availableBots = count($userData["star_botid"]);
            $num = min($num, $availableBots); // 确保 num 不超过可用的 star_botid 数量

            // 从 star_botid 中随机抽取 num 个
            $selectedBots = array_rand($userData["star_botid"], $num);
            $selectedBots = is_array($selectedBots) ? $selectedBots : [$selectedBots]; // 确保是数组

            foreach ($selectedBots as $botIndex) {
                $uid = $userData["star_botid"][$botIndex]; // $uid 是随机抽取的 botid
                $action->userLikeAction($uid, $nid, null, 1); // $nid 是 get 请求传入的值
            }
        }
    }

    $referrer = $_SERVER['HTTP_REFERER'];
    header("Location: $referrer");
    exit();

}


?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>

    <?php require_once('head.php'); ?>

    <style>
        .text-del-line{
            text-decoration: line-through;
        }
    </style>
</head>
<body>

<!--==================== Preloader Start ====================-->
<div class="preloader">
    <div class="loader"></div>
</div>
<!--==================== Preloader End ====================-->

<!--==================== Sidebar Overlay End ====================-->
<div class="side-overlay"></div>
<!--==================== Sidebar Overlay End ====================-->


<?php require_once('menu.php'); ?>
<div class="dashboard-main-wrapper">

    <?php require_once('top_navbar.php'); ?>
    <div class="dashboard-body">
        <!-- My Details Tab start -->
        <div class="tab-pane fade show active" id="pills-details" role="tabpanel" aria-labelledby="pills-details-tab" tabindex="0">
            <div class="card">
                <div class="card-header border-bottom">
                    <h4 class="mb-4"><?php echo $pageName?></h4>
                </div>
                <div class="card-body">
                    <form action="" method="get">
                        <div class="row gy-4">
                            <div class="col-sm-12 col-xs-12">
                                <label for="fname" class="form-label mb-8 h6">笔记noteId</label>
                                <input type="text" required maxlength="100" value="<?php echo $_GET['nid']?>" name="nid" class="form-control py-11" id="fname" placeholder="输入要操纵数据的笔记noteId">
                            </div>
                            <div class="col-12">
                                <div class="flex-align justify-content-end gap-8">
                                    <button type="submit" class="btn btn-main rounded-pill py-9">查询</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!-- My Details Tab End -->
        <?php if($_GET['nid']){?>

        <div class="card mt-24">
            <div class="card-header border-bottom">
                <div class="flex-between flex-wrap  gap-16">
                    <div>
                        <h4 class="mb-4">查询结果</h4>
                    </div>
                </div>
            </div>


            <div class="card-body">
                <div class="rounded-8 border border-gray-100 p-16 d-flex gap-12">
                    <div class="d-inline-flex w-100 max-w-130 flex-shrink-0">
                        <img src="<?php echo getFirstValidImage($res['image']);?>" alt="" class="rounded-6 cover-img max-w-130">
                    </div>
                    <div>
                        <p class="text-gray-600 text-line-3"><a href="/explore.php?id=<?php echo $res['note_id'];?>" class="<?php echo $res['is_view']==1 ? 'hover-text-main-600':'text-danger hover-text-danger-600 text-del-line' ;?>"><?php echo $res['title'];?></a></p>

                        <?php
                        $check_result = $res['check_result'];

                        switch ($check_result) {
                            case 0:
                                echo '<span class="text-13 py-2 px-10 rounded-pill bg-warning-50 text-warning-600 mb-16">审核中</span>';
                                break;
                            case 1:
                                echo '<span class="text-13 py-2 px-10 rounded-pill bg-success-50 text-success-600 mb-16">审核通过</span>';
                                break;
                            case 2:
                                echo '<span class="text-13 py-2 px-10 rounded-pill bg-danger-50 text-danger-600 mb-16">笔记违规</span>';
                                break;
                            default:
                                echo '<span class="text-13 py-2 px-10 rounded-pill bg-secondary-50 text-secondary-600 mb-16">未知状态</span>';
                                break;
                        }
                        echo $res['note_type'] == 1 ? '<span style="margin-left: 10px" class="text-13 py-2 px-10 rounded-pill bg-main-50 text-main-600 mb-16">视频</span>' : '<span style="margin-left: 10px" class="text-13 py-2 px-10 rounded-pill bg-pink-50 text-pink-600 mb-16">笔记</span>';
                        ?>
                        <a href="aibot.php?nid=<?php echo $res['note_id'];?>&num=5" class="btn btn-warning rounded-pill py-3 flex-center ">
                            AI评论 <span class="d-flex text-xl"><i class="ph ph-robot"></i></span>
                        </a>
                        <div class="flex-between gap-4 flex-wrap mt-20">

                            <a href="?admin_edit=<?php echo $res['note_id'];?>&uid=<?php echo $res['user_id'];?>" class="btn btn-outline-main rounded-pill py-3 flex-center ">
                                编辑 <span class="d-flex text-xl"><i class="ph ph-pencil-simple"></i></span>
                            </a>
                            <a href="/explore.php?id=<?php echo $res['note_id'];?>" class="btn btn-outline-main rounded-pill py-3 flex-center">
                                查看 <span class="d-flex text-xl"><i class="ph ph-arrow-right"></i></span>
                            </a>
                            <a onclick="return confirm('确定要<?php echo $res['is_view']==1 ? '隐藏':'恢复' ;?>这篇笔记吗？');" href="?<?php echo $res['is_view']==1 ? 'del':'recover' ;?>=<?php echo $res['note_id'];?>&uid=<?php echo $res['user_id'];?>" class="btn btn-success rounded-pill py-3 flex-center">
                                <?php echo $res['is_view']==1 ? '隐藏':'恢复' ;?> <?php echo $res['is_view']==1 ? '<span class="d-flex text-xl"><i class="ph ph-eye-slash"></i></span>':'<span class="d-flex text-xl"><i class="ph ph-hand-eye"></i></span>' ;?>
                            </a>
                            <a onclick="return confirm('确定要删除这篇笔记吗？不可恢复！删除后将连其他表的该ID一同删除清空！');" href="?realdel=<?php echo $res['note_id'];?>" class="btn btn-danger rounded-pill py-3 flex-center">
                                <span class="d-flex text-xl"><i class="ph ph-trash"></i></span> 删除
                            </a>
                        </div>
                        <div class="flex-center gap-4 flex-wrap mt-20">
                            <a href="?nid=<?php echo $res['note_id'];?>&status=1" class="btn bg-main-two-600 rounded-pill py-3 flex-center">
                                修改笔记审核为通过 <span class="d-flex text-xl"><i class="ph ph-eye"></i></span>
                            </a>
                            <a href="?nid=<?php echo $res['note_id'];?>&status=2" class="btn bg-purple-600 rounded-pill py-3 flex-center">
                                修改笔记审核为违规 <span class="d-flex text-xl"><i class="ph ph-smiley-x-eyes"></i></span>
                            </a>
                        </div>


                        <div class="flex-align gap-8 mt-24">
                            <img src="<?php echo '/' . ltrim($res["avatar"] ?: '/data/avatar/default.jpg', '/'); ?>" alt="" class="w-32 h-32 rounded-circle cover-img">
                            <span class="text-gray-600 text-13"><?php echo $res['nickname'];?></span>
                            <div class="flex-between gap-4">
                                <span class="text-15 fw-bold text-danger-600 d-flex"><i class="ph ph-heart"></i></span>
                                <span class="text-13 fw-bold text-gray-600"><?php echo $statist['likes_count'];?></span>
                            </div>
                            <div class="flex-between gap-4">
                                <span class="text-15 fw-bold text-warning-600 d-flex"><i class="ph ph-star"></i></span>
                                <span class="text-13 fw-bold text-gray-600"><?php echo $statist['favorites_count'];?></span>
                            </div>


                        </div>
                    </div>
                </div>
                <form action="" method="get">
                    <div class="row gy-4 mt-2">


                        <div class="col-sm-12 col-xs-12">
                            <label for="fname" class="form-label mb-8 h6">增减操作：</label>
                            <select id="userRole" name="addor" class="form-control py-11">
                                <option value="0">增加</option>
                                <option value="1">减少</option>
                            </select>
                        </div>
                        <div class="col-sm-12 col-xs-12">
                            <label for="fname" class="form-label mb-8 h6">数据类型：</label>
                            <select id="userRole" name="type" class="form-control py-11">
                                <option value="0">点赞数</option>
                                <option value="1">收藏数</option>
                            </select>
                        </div>
                        <div class="col-sm-12 col-xs-12">
                            <label for="fname" class="form-label mb-8 h6">数量：</label>
                            <input type="text" required maxlength="10000" value="" name="num" class="form-control py-11" id="fname" placeholder="输入要增减的数量">
                        </div>
                        <div class="col-sm-12 col-xs-12" style="display: none">
                            <label for="fname" class="form-label mb-8 h6">noteId：</label>
                            <input type="text" required value="<?php echo $_GET['nid']?>" name="nid" class="form-control py-11" id="fname" placeholder="noteId">
                        </div>
                        <div class="col-sm-12 col-xs-12">
                            <p for="fname" class="form-label mb-8 h6">当前对该笔记可用机器人进行增加点赞最大值为：<?php echo $bot_num["no_like_bot_num"]?> 个，可用收藏最大值为：<?php echo $bot_num["no_star_bot_num"]?> 个，已用机器人进行点赞量：<?php echo $bot_num["like_bot_num"]?> 个，收藏量：<?php echo $bot_num["star_bot_num"]?> 个</p>
                        </div>
                        <div class="col-12">
                            <div class="flex-align justify-content-end gap-8">
                                <a href="addbot.php" class="btn btn-success rounded-pill py-9">增加Bot</a>
                                <button type="submit" class="btn btn-main rounded-pill py-9">操作</button>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="flex-align justify-content-end gap-8">
                                <p for="fname" class="form-label mb-8 h6">全站Bot数：<?php echo $bot_num["total_bot"]?> 个</p>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <?php }?>


    </div>

    <?php require_once('footer.php'); ?>
</div>

<?php require_once('foot.php'); ?>



</body>
</html>